Group Object, Groups Collection, User Object, and Users Collection Example

This example illustrates the use of the Group and User objects and the Groups and Users collections. First, it creates a new User object and appends the object to the Users collection of the default Workspace object. Next, it creates a new Group object and appends the object to the Groups collection of the default Workspace object. Then the example adds user Pat Smith to the Accounting group. Finally, it enumerates the Users and Groups collections of the default Workspace object. See the methods and properties listed in the Group and User summary topics for additional examples.

Sub GroupX()

   Dim wrkDefault As Workspace
   Dim usrNew As User
   Dim usrLoop As User
   Dim grpNew As Group
   Dim grpLoop As Group
   Dim grpMember As Group

   Set wrkDefault = DBEngine.Workspaces(0)

   With wrkDefault

      ' Create and append new user.
      Set usrNew = .CreateUser("Pat Smith", _
         "abc123DEF456", "Password1")
      .Users.Append usrNew

      ' Create and append new group.
      Set grpNew = .CreateGroup("Accounting", _
         "UVW987xyz654")
      .Groups.Append grpNew

      ' Make the user Pat Smith a member of the 
      ' Accounting group by creating and adding the 
      ' appropriate Group object to the user's Groups 
      ' collection. The same is accomplished if a User 
      ' object representing Pat Smith is created and 
      ' appended to the Accounting group's Users 
      ' collection.
      Set grpMember = usrNew.CreateGroup("Accounting")
      usrNew.Groups.Append grpMember

      Debug.Print "Users collection:"

      ' Enumerate all User objects in the default
      ' workspace's Users collection.
      For Each usrLoop In .Users
         Debug.Print "  " & usrLoop.Name
         Debug.Print "    Belongs to these groups:"

         ' Enumerate all Group objects in each User
         ' object's Groups collection.
         If usrLoop.Groups.Count <> 0 Then
            For Each grpLoop In usrLoop.Groups
               Debug.Print "    " & _
                  grpLoop.Name
            Next grpLoop
         Else
            Debug.Print "    [None]"
         End If

      Next usrLoop

      Debug.Print "Groups collection:"

      ' Enumerate all Group objects in the default
      ' workspace's Groups collection.
      For Each grpLoop In .Groups
         Debug.Print "  " & grpLoop.Name
         Debug.Print "    Has as its members:"

         ' Enumerate all User objects in each Group
         ' object's Users collection.
         If grpLoop.Users.Count <> 0 Then
            For Each usrLoop In grpLoop.Users
               Debug.Print "    " & _
                  usrLoop.Name
            Next usrLoop
         Else
            Debug.Print "    [None]"
         End If

      Next grpLoop

      ' Delete new User and Group objects because this 
      ' is only a demonstration.
      .Users.Delete "Pat Smith"
      .Groups.Delete "Accounting"

   End With

End Sub